<!DOCTYPE html>
<html>
<head>
    <title>简单 JoinUser 测试</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/microsoft-signalr/6.0.1/signalr.min.js"></script>
</head>
<body>
    <h1>简单 JoinUser 测试</h1>
    <input type="text" id="username" value="TestUser" placeholder="用户名">
    <button onclick="testJoinUser()">测试加入用户</button>
    <div id="log" style="background: #f0f0f0; padding: 10px; margin: 10px 0; height: 300px; overflow-y: auto;"></div>

    <script>
        let connection = null;
        let currentUserId = null;

        // 日志函数
        function log(message) {
            const logDiv = document.getElementById('log');
            const timestamp = new Date().toLocaleTimeString();
            const entry = document.createElement('div');
            entry.textContent = `[${timestamp}] ${message}`;
            logDiv.appendChild(entry);
            logDiv.scrollTop = logDiv.scrollHeight;
            console.log(message);
        }

        // 初始化连接
        async function initConnection() {
            log('正在初始化 SignalR 连接...');
            
            connection = new signalR.HubConnectionBuilder()
                .withUrl('/signaling')
                .withAutomaticReconnect()
                .configureLogging(signalR.LogLevel.Debug)
                .build();

            // 注册事件处理器
            connection.on('UserJoined', (data) => {
                log(`✅ 收到 UserJoined 事件: ${JSON.stringify(data)}`);
                if (data.UserId) {
                    currentUserId = data.UserId;
                    log(`✅ 用户ID已保存: ${currentUserId}`);
                }
            });

            connection.on('Error', (message) => {
                log(`❌ 服务器错误: ${message}`);
            });

            connection.onconnectionstatechanged((state) => {
                log(`🔄 连接状态变更: ${state}`);
            });

            try {
                await connection.start();
                log('✅ SignalR 连接成功');
            } catch (err) {
                log(`❌ SignalR 连接失败: ${err}`);
            }
        }

        // 测试加入用户
        async function testJoinUser() {
            const username = document.getElementById('username').value;
            
            if (!connection) {
                log('❌ 连接未初始化');
                return;
            }

            if (connection.state !== signalR.HubConnectionState.Connected) {
                log(`❌ 连接状态不正确: ${connection.state}`);
                return;
            }

            log(`🚀 开始测试 JoinUser: ${username}`);
            log(`📊 调用前状态: currentUserId = ${currentUserId}`);

            try {
                log('📡 正在调用 JoinUser...');
                await connection.invoke('JoinUser', username);
                log('✅ JoinUser 调用完成');
                
                // 检查结果
                setTimeout(() => {
                    if (currentUserId) {
                        log(`🎉 测试成功！用户ID: ${currentUserId}`);
                    } else {
                        log('❌ 测试失败！未收到 UserJoined 事件或 currentUserId 为空');
                    }
                }, 2000);
                
            } catch (err) {
                log(`❌ JoinUser 调用失败: ${err}`);
            }
        }

        // 页面加载时初始化
        window.addEventListener('load', initConnection);
    </script>
</body>
</html> 